﻿using System;
using System.Collections.Generic;
using NTrace;
using System.Text;
using NUnit.Framework;

namespace NTrace.Tests
{
  [TestFixture]
  public class SimpleTests
  {
    [Test(Description = "Tests the simplest kind of tracing on a C# file")]
    public void SimpleTest1CS()
    {
      ProcessResource("NTrace.Tests.SimpleTest1.cs");
    }

    [Test(Description = "Tests the simplest kind of tracing on a VB.NET file")]
    public void SimpleTest1VB()
    {
      ProcessResource("NTrace.Tests.SimpleTest1.vb");
    }

    private static void ProcessResource(String resourceName)
    {
      Environment env = new Environment();
      env.EtwProviderVariable = "NTrace.Tests.TraceHelper.provider";
      env.OutputFolder = System.Environment.CurrentDirectory;
      FileProcessor processor = new FileProcessor(env);

      System.IO.Stream testSource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

      String tempFile = System.IO.Path.GetTempFileName();
      String tempSourceFile = System.IO.Path.ChangeExtension(tempFile, System.IO.Path.GetExtension(resourceName));
      System.IO.File.Move(tempFile, tempSourceFile);

      using (System.IO.StreamReader reader = new System.IO.StreamReader(testSource))
      {
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(tempSourceFile))
        {
          writer.Write(reader.ReadToEnd());
        }
      }

      String processedFile = null;
      try
      {
        processedFile = processor.Process(tempSourceFile);
      }
      finally
      {
        System.IO.File.Delete(tempSourceFile);
        if (processedFile != null)
        {
          System.IO.File.Delete(processedFile);
        }
      }
    }
  }
}
